/*
 * Copyright (c) 2011 Anthony Vandikas
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation 
 * files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, 
 * modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software 
 * is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 
 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE 
 * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

#include "stdafx.h"
#include "StudentListModel.h"

#include "AbstractStudentList.h"

StudentListModel::StudentListModel(AbstractStudentList &studentList)
	: studentList(&studentList)
{ 
	connect(&studentList, SIGNAL(modified()), this, SLOT(refresh()));
	sort(0, Qt::AscendingOrder);
}

QModelIndex StudentListModel::index(int row, int column, const QModelIndex &parent) const
{
	return parent.isValid() ? QModelIndex() : createIndex(row, column);
}

QModelIndex StudentListModel::parent(const QModelIndex &child) const
{
	return QModelIndex();
}

int StudentListModel::rowCount(const QModelIndex &parent) const
{
	return parent.isValid() ? 0 : students.size();
}

int StudentListModel::columnCount(const QModelIndex &parent) const
{
	return parent.isValid() ? 0 : 2;
}

QVariant StudentListModel::data(const QModelIndex &index, int role) const
{
	if (role != Qt::DisplayRole)
		return QVariant();

	auto student = students[index.row()];

	switch (index.column())
	{
	case 0:
		return student.getName();
	default: // 1
		return student.getIdentifier();
	}
}

QVariant StudentListModel::headerData(int section, Qt::Orientation orientation, int role) const
{
	if (role != Qt::DisplayRole)
		return QVariant();

	switch (section)
	{
	case 0:
		return "Name";
	default: // 1
		return "Id";
	}
}

void StudentListModel::sort(int column, Qt::SortOrder order)
{
	sortComparer = [=](const Student &left, const Student &right) -> bool
	{
		bool output;
		output = QString("Piers Liu") < QString("Salley Jeong");
		switch (column)
		{
		case 0:
			output = left.getName().localeAwareCompare(right.getName()) < 0;
			break;
		default:
			output = left.getIdentifier() < right.getIdentifier();
			break;
		}

		return order == Qt::AscendingOrder ? output : !output;
	};

	refresh();
}

void StudentListModel::refresh()
{
	students.clear();
	for (int i = 0; i < studentList->getSize(); i++)
		students << (*studentList)[i];

	qSort(students.begin(), students.end(), sortComparer);

	reset();
}

Student StudentListModel::getStudent(const QModelIndex &index) const
{
	return students[index.row()];
}
